package com.bluemoon.model.test.disrupter;

import com.lmax.disruptor.RingBuffer;
import org.springframework.util.SerializationUtils;

import java.nio.ByteBuffer;

/**
 * @author Jarod.Kong
 * @date 2021/3/18 9:38
 */
public class ModelTableEventProducer {
    public final RingBuffer<ModelTableEvent> ringBuffer;
    public ModelTableEventProducer(RingBuffer<ModelTableEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }
    public void onData(ByteBuffer byteBuffer) {
        // 1.ringBuffer 事件队列 下一个槽
        long sequence = ringBuffer.next();
        ModelTableEvent data = null;
        try {
            //2.取出空的事件队列
            ModelTableEvent longEvent = ringBuffer.get(sequence);
            byte[] b = byteBuffer.array();
            data = (ModelTableEvent)SerializationUtils.deserialize(b);
            //3.获取事件队列传递的数据
            if (data != null) {
                longEvent.setEntryModel(data.getEntryModel());
            }
            System.out.println(longEvent);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                //  Auto-generated catch block
                e.printStackTrace();
            }
        } finally {
            System.out.println("生产这准备发送数据");
            //4.发布事件
            ringBuffer.publish(sequence);
        }
    }
}
